let app = getApp();
Page({
    data: {
        header:{},
        userInfo:{},
        userheaderImageList:[],
        showList:[],
        selectType:'0',
        index:0,
        item:'武汉',
        current:1,
        pagesize:20,
        toTop:false,
        moreText:'加载更多...',
        totalPage:1,
        showA:false
    },
    onPullDownRefresh:function () {
      console.log('刷新')
      this.setData({
        showList:[],
        current:1,
        pagesize:20
      })
      this.getAllSheBei();
      this.getAllMember();
      var  timer=setTimeout(function(){
        clearTimeout(timer);
          wx.stopPullDownRefresh();
      },2000)
    },
    turnInvitation(){
      this.setData({
        showA:false
      })
     wx.navigateTo({
            url: '/pages/invitation/invitation'
        })
    },
    closeInvitation(){
      console.log('关闭')
      this.setData({
        showA:false
      })
    },
    //上拉加载更多
     onReachBottom:function(){
        console.log('加载更多')
        this.setData({
          current:this.data.current+1
        })
        if (this.data.current<=this.data.totalPage) {
          this.getAllSheBei();
        }else{
          this.setData({
            moreText:'没有更多了'
          })
        }
    },
    onShow:function(){
        wx.setStorageSync('chooseProvince',null);
        wx.setStorageSync('chooseCity',null);
       // this.setData({
       //  showList:_self.data.productList0
       // })
    },
    showLoading:function () {
      console.log('123231')
      this.setData({
        showLogin:true
      })
    },
    onCannel:function(){
     this.setData({
        showLogin:false
      })
    },
    onLoginSuccess:function () {
      this.setData({
        showLogin:false
      })
    },
    onLoad:function(){
     
      getApp().getUserInfo().then(res=>{
      var header={
             'openid':res.openid, //公用请求头
             'content-type': 'application/json'
         }
       this.setData({
        userInfo:res,
        header:header,
        current:1,
        showList:[]
       })  
      this.getAllSheBei();
      this.getAllMember();
      this.getBanner();
      this.getFloatView();
      this.getnewList();
     })

    },
    getFloatView(){
      var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl +'/yx/userScreen/isScreen?openid='+this.data.userInfo.openid,
            method:"GET",
            header: this.data.header,
            success: function(res) {
             console.log('res.data.obj*******')
             console.log(res.data.obj)
             _self.setData({
               showA:(res.data.obj==0?false:true)
             })
             
            
         }
       })
    },
    getnewList(){
        var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl +'/wx/articles/getNewList?openid='+this.data.userInfo.openid,
            method:"GET",
            header: this.data.header,
            success: function(res) {
             console.log('res.data.obj*******')
             console.log(res.data.obj)
             _self.setData({
               newList:res.data.obj
             })
             
            
         }
       })
    },
    getBanner(){
        var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl +'/wx/yxj/find/picture',
            method:"GET",
            header: this.data.header,
            success: function(res) {
             console.log(res.data.obj)
             _self.setData({
              bannerList:res.data.obj
             })
            
         }
       })
    },
    getAllMember:function () {
      var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl +'/wx/user/champion',
            method:"GET",
            header: this.data.header,
            success: function(res) {
             console.log(res.data.obj.list)
             _self.setData({
              userheaderImageList:res.data.obj.list
             })
            
         }
       })

    },
    getAllSheBei:function(type,province,city){
      console.log(123213)
      var url
      var province;
      if (province) {
          province=province;
      }else{
         province='湖北省'
      }
       if (city) {
          city=city;
      }else{
         city='武汉市'
      }
      if (type&&type!=0) {
         url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?status=20&type='+type+'&current='+this.data.current+'&pagesize='+this.data.pagesize
      }else{
        url=getApp().globalData.baseUrl + '/wx/yxj/equipment/getList?status=20'+'&current='+this.data.current+'&pagesize='+this.data.pagesize
      }
      
       var _self=this;
         wx.request({
            url: url,
            method:"GET",
            header: this.data.header,
            success: function(res) {
             console.log(res.data.obj.list)
             _self.setData({
              showList:_self.data.showList.concat(res.data.obj.list),
              totalPage:res.data.obj.pages
             })
           
           
         }
       })

    },
    onMyEvent:function(province){
      console.log(province.detail)
      
      this.getAllSheBei(this.data.selectType,province.detail.split(',')[0],province.detail.split(',')[1]);
    },
    getUserInfo:function(){

      
    },
    onPageScroll:function(e){
    console.log(e.scrollTop);//{scrollTop:99}
       if (e.scrollTop>500) {
        this.setData({
          toTop:true
        })
       }else{
          this.setData({
          toTop:false
        })
       }
    },
    moreEredarClick () { // 更多达人
        wx.navigateTo({
            url: '/pages/eredar/eredar'
        })
    },
    eredarClick (e) { // 达人详情
       // if (!this.data.userInfo.nickName) {
       //   this.setData({
       //    showLogin:true
       //   })
       //   return
       // }
      var userInfo= wx.getStorageSync('userInfo');
       if (!userInfo.nickName) {
         this.setData({
          showLogin:true
         })
  
         return
       }
      var id=e.target.dataset.code;
    
      wx.navigateTo({
            url: '/pages/eredarDetail/eredarDetail?openid='+id
        })
    },
    turnComponyDetail:function(e){
       if (!this.data.userInfo.nickName) {
         this.setData({
          showLogin:true
         })
         return
       }
      console.log(e.currentTarget.dataset.code)
      var type=e.currentTarget.dataset.code
      if(type==0){
         wx.navigateTo({
            url: '/pages/componyDetail/componyDetail?type=1'
        })
      }else{
           wx.navigateTo({
            url: '/pages/componyDetail2/componyDetail?type=1&name='+type
        })
      }
    },
    onShareAppMessage:function(){
         return {
              title: '上塔塔二手机买卖塔吊，免费发布设备，没有中间商赚差价',
              imageUrl: '' // 图片 URL
          }
    },
    onShareTimeline:function () {
       return {
              title: '上塔塔二手机买卖塔吊，免费发布设备，没有中间商赚差价',
              imageUrl: '' // 图片 URL
          }
    },
    turnPage:function(e){
         wx.pageScrollTo({
           scrollTop: 1
        })
    },
    turnTata:function(e){
      wx.switchTab({
        url: '/pages/index-s/index'
      })
    },
    turnSheQu:function(e){
       wx.switchTab({
        url: '/pages/community/community'
      })
    },
    chooseType:function(e){
       let type=e.target.dataset.type;
       console.log(e.target.dataset.type)
       this.setData({
        selectType:e.target.dataset.type
       })
        var _self=this;
       if (type=='0') {
       this.getAllSheBei();
       }else if(type=='10'){
       this.getAllSheBei(10);
       }
       else if(type=='20'){
       this.getAllSheBei(20);
       }
    },
    bindPickerChange:function(e){
        this.setData({
          index: e.detail.value
        })
    },
    showMyToast:function () {
       wx.navigateTo({
            url: '/pages/allCompony/allCompony'
        })
    },
    showMyToast2:function () {
       wx.navigateTo({
        url: '/pages/community/community'
      })
    },
    turnNewDetail:function(e){
      console.log(e)
      wx.navigateTo({
            url: '/pages/communityDetail/communityDetail?id='+e.currentTarget.dataset.model.id
        })
    }
});